package com.ffcs.cn.action.desktopcontrol.smpanelswitch;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.panels.SmPanelSwitch;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 可切换展示的面板控件的示例对话框
 *
  */
public class DialogSmPanelSwitch extends SmDialog {
	private SmPanelSwitch smPanelSwitch;
	private JButton buttonSwitch1;
	private JButton buttonSwitch2;
	private JButton buttonSwitchNull;
	private final ActionListener actionListener = e -> {
		if (e.getSource().equals(buttonSwitch1)) {
			smPanelSwitch.switchPanel("panel2");
		} else if (e.getSource().equals(buttonSwitch2)) {
			smPanelSwitch.switchPanel("panel1");
		} else {
			smPanelSwitch.switchToNull();
		}
	};

	public DialogSmPanelSwitch() {
		setTitle("可切换展示的面板控件");
		setSize(new Dimension(300, 400));
		initComponents();
		initLayout();
		registerEvents();
	}

	private void initComponents() {
		smPanelSwitch = new SmPanelSwitch();
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		panel1.add(new JLabel("面板1"), BorderLayout.CENTER);
		panel2.add(new JLabel("面板2"), BorderLayout.CENTER);
		// 添加两个不同面板
		smPanelSwitch.addPanel("panel1", panel1);
		smPanelSwitch.addPanel("panel2", panel2);
		// 根据设置的名称选取默认展示的面板
		smPanelSwitch.switchPanel("panel1");
		smPanelSwitch.setBorder(BorderFactory.createLineBorder(Color.black));
		buttonSwitch2 = new JButton("切换到面板1");
		buttonSwitch1 = new JButton("切换到面板2");
		buttonSwitchNull = new JButton("切换到空面板");

	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		JPanel buttonPanel = new JPanel(new GridBagLayout());
		buttonPanel.add(buttonSwitch1, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(0, 0)
				.setFill(GridBagConstraints.NONE));
		buttonPanel.add(buttonSwitch2, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(0, 0)
				.setFill(GridBagConstraints.NONE));
		buttonPanel.add(buttonSwitchNull, new GridBagConstraintsHelper(2, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(0, 0)
				.setFill(GridBagConstraints.NONE));
		this.add(buttonPanel, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 0)
				.setFill(GridBagConstraints.BOTH));
		this.add(smPanelSwitch, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private void registerEvents() {
		buttonSwitch1.addActionListener(actionListener);
		buttonSwitch2.addActionListener(actionListener);
		buttonSwitchNull.addActionListener(actionListener);
	}
}
